package com.free.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.free.common.security.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        if (getFieldValByName("createTime" , metaObject) == null) {
            this.strictInsertFill(metaObject, "createTime" , Date.class, new Date());
        }
        if (getFieldValByName("createBy" , metaObject) == null) {
            this.strictInsertFill(metaObject, "createBy" , String.class, SecurityUtils.getUserId()+"");
        }
        if (getFieldValByName("deleted" , metaObject) == null) {
            this.strictInsertFill(metaObject, "deleted" , String.class, "0");
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if (getFieldValByName("updateTime" , metaObject) == null) {
            this.strictUpdateFill(metaObject, "updateTime" , Date.class, new Date());
        }
        if (getFieldValByName("updateBy" , metaObject) == null) {
            this.strictUpdateFill(metaObject, "updateBy" , String.class, SecurityUtils.getUserId() +"");
        }
    }
}
